<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">

</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addInfo"> 添加账号 </button>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="setNumber"> 设置点赞和评论次数 </button>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="deletegoods"> 删除勾选 </button>
                    <input type="file" id="excelFile" accept=".xls,.xlsx" style="display: none;" />
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


            <script type="text/html" id="currentTableBar">
            </script>

        </div>
    </div>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/lib/axios/axios.js" charset="utf-8"></script>
    <script>

        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            let tableIns = table.render({
                elem: '#currentTableId',
                url: '/account/getDyData',
                toolbar: '#toolbarDemo',
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'account', minWidth: 100, title: '账号', sort: true },
                    { field: 'likeNumber', minWidth: 100, title: '可点赞次数', sort: true },
                    { field: 'commentNumber', minWidth: 100, title: '可评论次数', sort: true },
                ]],
                limits: [50, 200, 1000, 2000, 5000],
                limit: 50,
                page: true
            });

            var laydate = layui.laydate;
            /**
             * toolbar监听事件
             */

            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event == 'addInfo') {
                    layer.prompt({
                        formType: 2,
                        value: "",
                        title: "一行一条",
                        maxlength: 500000,
                        area: ['700px', '500px'] //自定义文本域宽高
                    }, function (value, index, elem) {
                        layer.close(index);
                        let valueArr = value.split("\n")
                        console.log(valueArr)

                        $.ajax({
                            url: "/account/setNewDy",
                            data: {
                                dyInfoArr: JSON.stringify(valueArr),
                            },
                            type: "POST",
                            beforeSend: function (obj) {
                                layer.msg("添加中...", {
                                    icon: 16,
                                    time: -1
                                })
                            },
                            success: function (data) {
                                layer.closeAll('loading'); //关闭loading
                                layer.msg("添加结果:" + data.msg)
                                tableIns.reload({
                                    url: '/account/getDyData',
                                    method: 'get',
                                })
                            },
                            error: function (xhr, textstatus, thrown) {
                                layer.msg('接口有误');
                            }
                        })
                    });
                } else if (obj.event == 'resetInfo') {
                    let checkStatus = table.checkStatus('currentTableId')
                    let data = checkStatus.data;
                    if (data.length == 0) {
                        return
                    }

                    layer.confirm('真的重置这些数据么?', function (index) {
                        console.log(data)
                        let idArr = []
                        data.forEach(d => {
                            idArr.push(d._id)
                        })
                        $.ajax({
                            url: '/account/setDyStatus',
                            type: 'post',
                            data: {
                                idArr: JSON.stringify(idArr)
                            },
                            success: function () {
                                layer.msg("重置成功")
                                tableIns.reload({
                                    url: '/account/getDyData',
                                    method: 'get',
                                })
                            },
                            error: function (xhr, textstatus, thrown) {
                                layer.msg('接口有误');
                            }
                        })
                        layer.close(index);
                    });
                } else if (obj.event == 'deletegoods') {
                    let checkStatus = table.checkStatus('currentTableId')
                    let data = checkStatus.data;
                    if (data.length == 0) {
                        return
                    }

                    layer.confirm('真的删除这些数据么?', function (index) {
                        console.log(data)
                        let idArr = []
                        data.forEach(d => {
                            idArr.push(d._id)
                        })
                        $.ajax({
                            url: '/account/deleteData',
                            type: 'post',
                            data: {
                                idArr: JSON.stringify(idArr)
                            },
                            success: function () {
                                layer.msg("删除成功!")
                                tableIns.reload({
                                    url: '/account/getDyData',
                                    method: 'get',
                                })
                            },
                            error: function (xhr, textstatus, thrown) {
                                layer.msg('接口有误');
                            }
                        })
                        layer.close(index);
                    });
                }else if(obj.event == 'setNumber'){
                    layer.open({
                        title: '设置可点赞数量和可评论数量',
                        type: 1,
                        area: ['350px', '220px'], // 自定义弹出框宽高
                        content: `
                            <form class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">可点赞数量</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="likeNumber" required lay-verify="required" placeholder="请输入可点赞数量" autocomplete="off" class="layui-input"></input>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">可评论数量</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="commentNumber" required lay-verify="required" placeholder="请输入可评论数量" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button type="button" class="layui-btn" id="saveBtn1">保存</button>
                                    </div>
                                </div>
                            </form>
        `,
                        success: function (layero, index) {
                            layui.use(['form'], function () {
                                var form = layui.form;

                                // 重新渲染表单
                                form.render();

                                // 绑定保存按钮的点击事件
                                $(layero).find('#saveBtn1').on('click', function () {
                                    let newLikeNumber = $(layero).find('input[name="likeNumber"]').val().trim();
                                    let newCommentNumber = $(layero).find('input[name="commentNumber"]').val().trim();

                                    if (newLikeNumber.length <= 0) {
                                        return layer.msg('点赞数量不能为空！');
                                    }
                                    if (newCommentNumber.length <= 0) {
                                        return layer.msg('评论数量不能为空！');
                                    }

    

                                    // 发送 PUT 请求更新数据
                                    axios.post('/account/setNumber', {
                                        likeNumber: newLikeNumber,
                                        commentNumber: newCommentNumber
                                    })
                                        .then(function (res) {
                                            layer.closeAll(); // 关闭所有弹出框
                                            tableIns.reload(); // 刷新表格
                                            layer.msg('更新成功！');
                                        })
                                        .catch(function (err) {
                                            console.log("请求失败", err.response || err);
                                            layer.alert(err.message || "请求失败");
                                        });
                                });
                            });
                        }
                    });
                }
            });


            table.on('tool(currentTableFilter)', function (obj) {
                const data = obj.data
                if (obj.event === 'deleteOne') {
                    layer.confirm('真的删除【' + data.account + '】么?', function (index) {
                        obj.del();
                        tableIns.reload({
                            url: '/account_table/delOne',
                            method: 'post',
                            where: {
                                _id: data._id
                            }
                        })
                        layer.close(index);
                    });
                }
            });

            

        });
    </script>

</body>

</html>